#!/usr/bin/python3
import urllib.request
import urllib.parse
# import ssl
import json
import re
import hashlib
import time
import demo
import os


def translate(fromLang, toLang, filePath):
    # fromLang = 'zh'  # 中文
    # toLang = 'en'  # 英文
    #
    # filePath = "/home/lv/AndroidStudioProjects/Uboex/tibi/src/main/res/values"

    language = {"en": "-en", "cht": "-zh-rTW", "kor": "-ko", "jp": "-ja"}

    f = open(filePath + '/strings.xml', 'r', encoding='UTF-8')  # 读模式
    line = f.readline()
    all = ''
    while line:
        print(line)
        zhPattern = re.compile(r'>([\s\S]*?)<')
        match = zhPattern.findall(line)
        if len(match) != 0:
            zhPattern1 = re.compile(u'[\u4e00-\u9fa5]+')  # 查找汉字
            match1 = zhPattern1.search(match[0])
            if match1:
                print(match[0])
                text = demo.huangBaidu(match[0], toLang)
                all = all + line.replace(match[0], text)
            else:
                print("本行没有汉字")
                all = all + line
        else:
            print("本行没有尖括号")
            all = all + line
        line = f.readline()
    f.close()
    try:
        os.makedirs(filePath + language[toLang])
    except Exception as e:
        pass
    strFile = open(filePath + language[toLang] +
                   "/strings.xml", 'w+', encoding='UTF-8')
    read_data = strFile.read()
    strFile.seek(0)
    strFile.truncate()
    strFile.write(all)


translateLan = ["en"]
models = ["app", "fabi", "hangqing", "ident", "invite",
          "level", "lib", "login", "mine", "pay", "tibi"]
# models = ["app"]
projectPath = "/home/lv/AndroidStudioProjects/Btoex/"
resPath = "/src/main/res/values"
for lan in translateLan:
    for model in models:
        filepath = projectPath + model + resPath
        translate("zh", lan, filepath)
# print(demo.huangBaidu("交易额：首页", "en"))
